/**
 * 监听开始时间与结束控件的变化，自动算出天数
 */
$(function(){
	var userInfo = {};
	$.ajax({
		method: 'post',
	    url: basePath() + 'user/getUser.do',
	    async: false,
		contentType : 'application/json',
		data:JSON.stringify({}),
		dataType: 'json',
		success: function(data){
			if(data.result=="0"){
				$.alert({
				    title: '提示!',
				    content: '请先登录',
				    confirmButton: '确定'
				});
					
			}else{
				userInfo = data;
			}
		}
	});

	if(!jQuery.isEmptyObject(userInfo) && itemno != '1002' && itemno != '1005'){
		if($('[name="contactidcard"]').length > 0 ){
			$('[name="contactidcard"]').val(userInfo.userid);
		}
		
		if($('[name="agentidcard"]').length > 0 ){
			$('[name="agentidcard"]').val(userInfo.userid);
		}
		
		/*if($('[name="buildunitname"]').length > 0 ){
			$('[name="buildunitname"]').val(userInfo.company);
		}
		
		if($('[name="workunitname"]').length > 0){
			$('[name="workunitname"]').val(userInfo.company);
		}*/

		if($('[name="buildcontactperson"]').length > 0){
			$('[name="buildcontactperson"]').val(userInfo.proposer);
		}
		
		if($('[name="workcontactperson"]').length > 0){
			$('[name="workcontactperson"]').val(userInfo.proposer);
		}
		
		if($('[name="contactperson"]').length > 0){
			$('[name="contactperson"]').val(userInfo.proposer);
		}
		
		if($('[name="agentperson"]').length > 0){
			$('[name="agentperson"]').val(userInfo.proposer);
		}
		
		if($('[name="buildcontactphone"]').length > 0){
			$('[name="buildcontactphone"]').val(userInfo.mobilephone);
		}
		
		if($('[name="agentphone"]').length > 0){
			$('[name="agentphone"]').val(userInfo.mobilephone);
		}
		
		if($('[name="contactphone"]').length > 0){
			$('[name="contactphone"]').val(userInfo.mobilephone);
		}
		
		if($('[name="workcontactphone"]').length > 0){
			$('[name="workcontactphone"]').val(userInfo.mobilephone);
		}
		
	}
	
	$('[name="workbegintime"]').change(function(){
		
		if($('[name="days"]').length > 0 && $('[name="workendtime"]').length > 0){
			if($('[name="workendtime"]').val() != ''){
				var strDate1 = $('[name="workbegintime"]').val().replace(/-/g,"/"); 
				var strDate2 = $('[name="workendtime"]').val().replace(/-/g,"/"); 
				var date1 = Date.parse(strDate1); 
				var date2 = Date.parse(strDate2); 
				$('[name="days"]').val(Math.ceil((date2-date1)/(24*60*60*1000)) + 1);
			} else {
				$('[name="workendtime"]').attr('min', $('[name="workbegintime"]').val());
			}
		}
	});
	
	$('[name="workendtime"]').change(function(){
		if($('[name="days"]').length > 0 && $('[name="workbegintime"]').length > 0){
			if($('[name="workbegintime"]').val() != ''){
				var strDate1 = $('[name="workbegintime"]').val().replace(/-/g,"/"); 
				var strDate2 = $('[name="workendtime"]').val().replace(/-/g,"/"); 
				var date1 = Date.parse(strDate1); 
				var date2 = Date.parse(strDate2); 
				$('[name="days"]').val(Math.ceil((date2-date1)/(24*60*60*1000)) + 1);
			} else {
				$('[name="workbegintime"]').attr('max', $('[name="workendtime"]').val());
			}
		}
	});
	
	$(function(){
		$(".hiddenField").hide();
	});
});
